package com.aiju.base.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.aiju.base.activity.AbsActivity;
import com.aiju.app.config.ConfigManager;
import com.aiju.support.lifecycle.ActivityFragmentLifecycle;
import com.aiju.utils.L;

import static com.aiju.utils.L.isDebug;


/**
 * @auther: 佐为
 * @date: 2016.8.15
 * @Description: 第一层抽象的fragment基类
 */

public abstract class AbsFragment extends Fragment {

    private final boolean DEBUG = ConfigManager.DEBUG;

    private final ActivityFragmentLifecycle lifecycle = new ActivityFragmentLifecycle();

    public AbsActivity getContext() {
        return (AbsActivity) getActivity();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        log("onAttach()");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log("onCreate()");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        log("onActivityCreated()");
    }

    @Override
    public void onStart() {
        super.onStart();
        log("onStart()");
        lifecycle.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        log("onResume()");
    }

    @Override
    public void onStop() {
        super.onStop();
        log("onStop()");
        lifecycle.onStop();
    }

    @Override
    public void onDestroy() {
        log("onDestroy()");
        lifecycle.onDestroy();
        super.onDestroy();
    }

    /**
     * 打印日志
     *
     * @param msg
     */
    private void log(String msg) {
        if (isDebug) {
            L.d(ConfigManager.DEBUG_LIFECYCLE_TAG, getClass().getName() + ":" + msg);
        }
    }

    public ActivityFragmentLifecycle getLifecycle() {
        return lifecycle;
    }
}
